package com.hh.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import java.util.HashMap;

public class PageTag extends TagSupport {
    private Integer pageNo;
    private Integer pageSize;
    private Integer total;
    private Integer totalPage;
    @Override
    public int doStartTag() throws JspException {
        JspWriter out = super.pageContext.getOut();
        String strLi="";
        if(null==totalPage){
            return 0;
        }
        int pageIndex=pageNo==1||pageNo==2?3:pageNo;
        boolean startFlag=totalPage<6;
        boolean endFlag=totalPage>5&&pageNo+3>totalPage;
        for(int i=pageIndex-2;i<pageIndex+4;i++){
            if(pageNo==i){
                strLi+="<li class='paging-label' data-value="+i+">"+i+"</li>";
            }else{
                strLi+="<li data-value="+i+">"+i+"</li>";
            };
        }
        if(startFlag){
            strLi="";
            for(int i=1;i<totalPage+1;i++){
                if(pageNo==i){
                    strLi+="<li class='paging-label' data-value="+i+">"+i+"</li>";
                }else{
                    strLi+="<li data-value="+i+">"+i+"</li>";
                }
            }
        }
        if(endFlag){
            strLi="";
            for(int i=totalPage-5;i<totalPage+1;i++){
                if(pageNo==i){
                    strLi+="<li class='paging-label' data-value="+i+">"+i+"</li>";
                }else{
                strLi+="<li data-value="+i+">"+i+"</li>";
                }
            }
        }
        String pageHtml=" <div id='datapager' class='datapager'>" +
                " <ul>" +
                "<li data-value="+(this.pageNo>1?(this.pageNo-1):1)+">&laquo;</li>" +
                strLi +
                "<li data-value="+(this.pageNo<this.totalPage?(this.pageNo+1):this.totalPage)+">&raquo;</li> " +
                "</ul>" +
                "</div>";
        try {
            out.println(pageHtml);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    /*@Override
    public int doEndTag() throws JspException {
        return 6;
    }*/

    public Integer getPageNo() {
        return pageNo;
    }

    public void setPageNo(Integer pageNo) {
        this.pageNo = pageNo;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;

    }

    public Integer getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(Integer totalPage) {
        this.totalPage = totalPage;
    }
}
